﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using winS.Unity.Process;
using winS.UnityEditor.UIElement;
using Label = winS.UnityEditor.UIElement.Label;

namespace winS.UnityEditor.ProcessEditor
{
    /// <summary>
    /// 文件设置窗口
    /// </summary>
    public class FileSettingWindow : VisualElement
    {
        private static readonly float widthPercent = 14f;

        private static readonly Dictionary<EditorData.DataSaveTargetType, string> dataSaveTargetTypeAlias = new Dictionary<EditorData.DataSaveTargetType, string>();

        public bool hidden { get; private set; } = true;

        private Label nameLabel;
        private Label pathLabel;

        static FileSettingWindow()
        {
            dataSaveTargetTypeAlias.Add(EditorData.DataSaveTargetType.SaveToLocal, "保存在本机中");
            dataSaveTargetTypeAlias.Add(EditorData.DataSaveTargetType.SaveToFile, "保存在文件中");
        }

        internal FileSettingWindow()
        {
            focusable = true;
            style.overflow = Overflow.Hidden;
            style.position = Position.Absolute;
            Hide();
            style.SetTransition("left", 0.1f);
            style.SetWidth(widthPercent, LengthUnit.Percent);
            style.SetHeight(100f, LengthUnit.Percent);
            style.SetBorderRight(1f, Color.black);
            style.SetBackgroundColor(ColorLevelUtility.GetColor(2));
            style.SetPadding(4f);
            style.paddingLeft = 6f;
        }

        public void Show(EditorProcessInfo editorProcessInfo)
        {
            hidden = false;
            LoadProcessInfo(editorProcessInfo);
            Focus();
            style.SetLeft(0f, LengthUnit.Percent);
        }
        public void Hide()
        {
            hidden = true;
            style.SetLeft(-widthPercent, LengthUnit.Percent);
        }

        public void LoadProcessInfo(EditorProcessInfo editorProcessInfo)
        {
            if (hidden) return;
            Clear();
            IProcess process = editorProcessInfo.process;
            EditorData processEditorData = process.editorData;
            Label titleLabel = CreateLabel("文件信息");
            titleLabel.style.fontSize = 16f;
            titleLabel.style.unityFontStyleAndWeight = FontStyle.Bold;
            Add(titleLabel);

            Add(nameLabel = CreateLabel($"文件名称:  {editorProcessInfo.name}"));
            Add(pathLabel = CreateLabel($"文件路径:  {editorProcessInfo.pathRelativeProject}"));
            editorProcessInfo.onUpdated += () =>
            {
                nameLabel.text = editorProcessInfo.name;
                pathLabel.text = editorProcessInfo.pathRelativeProject;
            };

            Add(CreateLabel($"流程类型:  {editorProcessInfo.processType.Name}"));
            Add(CreateLabel($"流程编辑器类型:  {editorProcessInfo.editorType.Name}"));

            Add(EditorElementUtility.elementFactory.CreateEnumField("视图数据保存点:", processEditorData.viewDataSaveTargetType, dataSaveTargetTypeAlias, newValue => processEditorData.viewDataSaveTargetType = newValue));
        }

        private Label CreateLabel(string labelText)
        {
            Label label = EditorElementUtility.elementFactory.CreateLabel(labelText, TextAnchor.MiddleLeft);
            label.style.alignSelf = Align.FlexStart;
            label.style.marginLeft = EditorElementUtility.defaultMarginHorizontal;
            label.style.whiteSpace = WhiteSpace.Normal;
            return label;
        }
    }
}
